package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpLogMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;

import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult<Emp> findPage(Integer page, Integer pageSize) {
        // Long total 这个表的总共有多少数据
        //select count(*) from emp
//        Long total = empMapper.findCount();
//        Integer skip = (page - 1) * pageSize;
//        List<Emp> rows = empMapper.findPage(skip, pageSize);
        PageHelper.startPage(page, pageSize);
        Page<Emp> page1 = (Page<Emp>) empMapper.list();
        long total = page1.getTotal();
        List<Emp> rows = page1.getResult();
        return new PageResult<Emp>(total, rows);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) throws Exception {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");
        //2.保存员工基本信息
        empMapper.insert(emp);
//        if (true) {
//            throw new Exception("异常出现了");
//        }
        EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
        empLogService.insertLog(empLog);


        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && exprList.size() > 0) {
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
        }

        empExprMapper.saveBatch(exprList);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(@RequestParam("ids") List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.getById(id);
        List<EmpExpr> exprList = empExprMapper.getByEmpId(id);
        emp.setExprList(exprList);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && exprList.size() > 0) {
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.saveBatch(exprList);
        }
    }

    @Override
    public JobOption empJobData() {
        List<Map> mapList = empMapper.emJobData();
        JobOption jobOption = new JobOption();
        List jobList = new ArrayList();
        List dataList = new ArrayList();
        for (Map map : mapList) {
            jobList.add(map.get("jobName"));
            dataList.add(map.get("nums"));

        }
        jobOption.setJobList(jobList);
        jobOption.setDataList(dataList);
        return jobOption;
    }

    @Override
    public List<Map> empGenderData() {
        return empMapper.empGenderData();
    }


}